package com.evil.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evil.application.mapper.ApplicationNotifySettingMapper;
import com.evil.application.pojo.dto.notify.CopyAppNotifySettingReqDTO;
import com.evil.application.pojo.entity.ApplicationInfo;
import com.evil.application.pojo.entity.ApplicationNotifySetting;
import com.evil.application.service.ApplicationNotifySettingService;
import com.evil.common.application.enums.ApplicationVersionEnum;
import com.evil.common.core.enums.SwitchEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

/**
 * 应用提醒设置表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ApplicationNotifySettingServiceImpl extends ServiceImpl<ApplicationNotifySettingMapper, ApplicationNotifySetting> implements ApplicationNotifySettingService {

    private final ApplicationLogServiceImpl logService;

    /**
     * 初始化应用提醒设置
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initNotifySetting(ApplicationInfo applicationInfo) {
        log.info("【初始化应用提醒设置】，参数：{}", applicationInfo.getApplicationId());

        // 先初始化设计版
        ApplicationNotifySetting designNotifySetting = this.baseMapper.isExistByAppIdAndVersion(
                        applicationInfo.getApplicationId(), ApplicationVersionEnum.DESIGN_VERSION.getId())
                // 不存在得补
                .orElseGet(() -> this.createApplicationNotifySetting(applicationInfo, ApplicationVersionEnum.DESIGN_VERSION));

        // 先初始化发布版
        ApplicationNotifySetting releaseNotifySetting = this.baseMapper.isExistByAppIdAndVersion(
                        applicationInfo.getApplicationId(), ApplicationVersionEnum.RELEASE_VERSION.getId())
                // 不存在得补
                .orElseGet(() -> this.createApplicationNotifySetting(applicationInfo, ApplicationVersionEnum.RELEASE_VERSION));

        // 然后发布 设计版 -> 发布版
        this.replaceApplicationNotifySetting(designNotifySetting, releaseNotifySetting);
    }

    /**
     * 删除应用提醒设置
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void deleteNotifySetting(ApplicationInfo applicationInfo) {
        ApplicationVersionEnum.forEach(em -> {
            ApplicationNotifySetting applicationNotifySetting = this.baseMapper.findByIdAppIdAndVersion(applicationInfo.getApplicationId(), em.getId());
            applicationNotifySetting.setIsDeleted(SwitchEnum.YES.getId());

            logService.saveAndCheck(this.baseMapper, ApplicationNotifySetting::getApplicationNotifySettingId, applicationNotifySetting);
        });
    }

    /**
     * 复制应用提醒设置
     *
     * @param copyAppNotifySettingReqDTO copyAppNotifySettingReqDTO
     */
    @Override
    public void copyNotifySetting(CopyAppNotifySettingReqDTO copyAppNotifySettingReqDTO) {
        ApplicationInfo source = copyAppNotifySettingReqDTO.getSource();
        ApplicationInfo target = copyAppNotifySettingReqDTO.getTarget();

        ApplicationVersionEnum.forEach(em -> {
            ApplicationNotifySetting applicationNotifySetting = this.baseMapper.findByIdAppIdAndVersion(source.getApplicationId(), em.getId());

            ApplicationNotifySetting copyNotifySetting = BeanUtil.copyProperties(applicationNotifySetting, ApplicationNotifySetting.class);
            copyNotifySetting.setApplicationNotifySettingId(null);
            copyNotifySetting.setApplicationId(target.getApplicationId());

            logService.saveAndCheck(this.baseMapper, ApplicationNotifySetting::getApplicationNotifySettingId, copyNotifySetting);
        });
    }

    /**
     * 发布应用提醒设置
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void publishApplicationNotifySetting(ApplicationInfo applicationInfo) {
        this.replaceApplicationNotifySetting(applicationInfo, true);
    }

    /**
     * 还原应用提醒设置
     *
     * @param applicationInfo applicationInfo
     */
    @Override
    public void restoreApplicationNotifySetting(ApplicationInfo applicationInfo) {
        this.replaceApplicationNotifySetting(applicationInfo, false);
    }

    /**
     * 替换提醒设置信息
     *
     * @param applicationInfo applicationInfo
     * @param isPublish       isPublish
     */
    private void replaceApplicationNotifySetting(ApplicationInfo applicationInfo, boolean isPublish) {
        // 发布版
        int releaseVersion = ApplicationVersionEnum.RELEASE_VERSION.getId();
        // 设计版
        int designVersion = ApplicationVersionEnum.DESIGN_VERSION.getId();
        ApplicationNotifySetting releaseNotifySetting = this.baseMapper.findByIdAppIdAndVersion(applicationInfo.getApplicationId(), releaseVersion);
        ApplicationNotifySetting designNotifySetting = this.baseMapper.findByIdAppIdAndVersion(applicationInfo.getApplicationId(), designVersion);

        if (isPublish) {
            // 发布：设计版 -> 发布版
            this.replaceApplicationNotifySetting(designNotifySetting, releaseNotifySetting);
        } else {
            // 还原：发布版 -> 设计版
            this.replaceApplicationNotifySetting(releaseNotifySetting, designNotifySetting);
        }
    }

    /**
     * 替换提醒设置信息
     *
     * @param source source
     * @param target target
     */
    private void replaceApplicationNotifySetting(ApplicationNotifySetting source, ApplicationNotifySetting target) {
        target.setNotifyPending(source.getNotifyPending());
        target.setNotifyResult(source.getNotifyResult());
        target.setNotifyCarbonCopy(source.getNotifyCarbonCopy());
        target.setNotifyFallback(source.getNotifyFallback());
        target.setNotifyPromotion(source.getNotifyPromotion());
        target.setNotifyMessage(source.getNotifyMessage());
        target.setNotifyOvertime(source.getNotifyOvertime());
        target.setWxNotify(source.getWxNotify());
        target.setMailNotify(source.getMailNotify());

        logService.saveAndCheck(this.baseMapper, ApplicationNotifySetting::getApplicationNotifySettingId, target);
    }

    /**
     * 创建申请设置
     *
     * @param applicationInfo applicationInfo
     * @param em              em
     * @return ApplicationNotifySetting
     */
    private ApplicationNotifySetting createApplicationNotifySetting(ApplicationInfo applicationInfo, ApplicationVersionEnum em) {
        ApplicationNotifySetting applicationNotifySetting = new ApplicationNotifySetting();
        applicationNotifySetting.setEnterpriseId(applicationInfo.getEnterpriseId());
        applicationNotifySetting.setApplicationId(applicationInfo.getApplicationId());
        applicationNotifySetting.setApplicationVersion(em.getId());

        applicationNotifySetting.setNotifyPending(SwitchEnum.NO.getId());
        applicationNotifySetting.setNotifyResult(SwitchEnum.NO.getId());
        applicationNotifySetting.setNotifyCarbonCopy(SwitchEnum.NO.getId());
        applicationNotifySetting.setNotifyFallback(SwitchEnum.NO.getId());
        applicationNotifySetting.setNotifyPromotion(SwitchEnum.NO.getId());
        applicationNotifySetting.setNotifyMessage(SwitchEnum.NO.getId());
        applicationNotifySetting.setNotifyOvertime(SwitchEnum.NO.getId());
        applicationNotifySetting.setWxNotify(SwitchEnum.NO.getId());
        applicationNotifySetting.setMailNotify(SwitchEnum.NO.getId());

        applicationNotifySetting.setIsDeleted(SwitchEnum.NO.getId());

        logService.saveAndCheck(this.baseMapper, ApplicationNotifySetting::getApplicationNotifySettingId, applicationNotifySetting);

        return applicationNotifySetting;
    }
}
